<?php

require("./mpcamera/setup.php");

$smarty = new Smarty_MPCamera();

$action = strtolower(filter_input(INPUT_GET, "action"));
$submit = strtolower(filter_input(INPUT_POST, "submit"));

if ($action == "view") {
    $saleView = $smarty->getSaleViews();
    $smarty->assign("saleView", $saleView);
}

if ($action == "edit") {
    if (empty($submit)) {
        $id = $smarty->clean(filter_input(INPUT_GET, "id"), true);
        if ($id > 0) {
            $saleItem = $smarty->getSaleItemByID($id);
            $smarty->assign("saleItem", $saleItem);
        } else {
            header("Location: /sale/view");
        }
    } else if ($submit == "update") {
        $cbbTrafficType = filter_input(INPUT_POST, "cbbTrafficType");
        $txtQuantityTable = $smarty->clean(filter_input(INPUT_POST, "txtQuantityTable"), true);
        $txtQuantityLCD = $smarty->clean(filter_input(INPUT_POST, "txtQuantityLCD"), true);
        $txtPeopleWeek = $smarty->clean(filter_input(INPUT_POST, "txtPeopleWeek"), true);
        $txtPeopleDay = $smarty->clean(filter_input(INPUT_POST, "txtPeopleDay"), true);
        $txtViewerDay = $smarty->clean(filter_input(INPUT_POST, "txtViewerDay"), true);
        $maSI = $smarty->clean(filter_input(INPUT_POST, "maSI"), true);

        $result = $smarty->setSaleEdit($maSI, $cbbTrafficType, $txtQuantityTable, $txtQuantityLCD, $txtPeopleWeek, $txtPeopleDay, $txtViewerDay);

        if ($result) {
            $success = 1;
        } else {
            $success = 0;
        }
        $smarty->assign("success", $success);
    }
}

if ($action == "export") {

    $data = $smarty->getSaleViews();

    // Load template from file
    $objPHPExcel = PHPExcel_IOFactory::load('ReportForSale.xlsx');

    // Settings
    if (true) {
        $begin_row = 10;
        $end_row = $begin_row + count($data) - 1;
        $col = 11;
        $objPHPExcel->setActiveSheetIndex(0);
    }

    // Template
    if (true) {
        // Biên trái của bảng - nét liền đậm
        $objPHPExcel->getActiveSheet()->getStyle('A' . $begin_row . ':A' . $end_row)->getBorders()->getLeft()->setBorderStyle('medium');
        // Biên phải của bảng - nét liền đậm
        $objPHPExcel->getActiveSheet()->getStyle('K' . $begin_row . ':K' . $end_row)->getBorders()->getRight()->setBorderStyle('medium');
        // Biên phải của cột - nét liền
        for ($char = 'A'; $char < 'K'; $char++) {
            $objPHPExcel->getActiveSheet()->getStyle($char . $begin_row . ':' . $char . $end_row)->getBorders()->getRight()->setBorderStyle('thin');
        }
        // Biên dưới của hàng - nét chấm bi
        for ($row = $begin_row; $row < $end_row; $row++) {
            $objPHPExcel->getActiveSheet()->getStyle('A' . $row . ':K' . $row)->getBorders()->getBottom()->setBorderStyle('hair');
        }
        // Biên dưới của bảng - nét liền đậm
        $objPHPExcel->getActiveSheet()->getStyle('A' . $end_row . ':K' . $end_row)->getBorders()->getBottom()->setBorderStyle('medium');
    }

    // Data
    if (true) {
        $count = 1;
        foreach ($data as $item) {
            $char = 'A';
            for ($i = 0; $i < 11; $i++) {
                if ($i == 0) {
                    $objPHPExcel->getActiveSheet()->setCellValue($char++ . $begin_row, $count++);
                } else {
                    $objPHPExcel->getActiveSheet()->setCellValue($char++ . $begin_row, $item[$i]);
                }
            }
            $begin_row++;
        }
    }
    // Redirect output to a client’s web browser (Excel5) 
    if (true) {
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $date = date("Y-m-d");
        $filename = "ReportForSale_$date.xls";
        header("Content-Type: application/vnd.ms-excel");
        header("Content-Disposition: attachment;filename=$filename");
        header("Cache-Control: max-age=0");

        $objWriter->save('php://output');
    }
}

$smarty->assign("action", $action);
$smarty->display("sale.tpl");
